﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>微信支付</title>
    <script src="../Content/Script/jquery.min.js"></script>

</head>
<body>

</body>
    <script type="text/javascript">
        window.onload=function(){
            callpay();
        }

        function callpay() {
            if (typeof ('WeixinJSBridge') == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }
                else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }
            else {
                fPostCharge();
            }
        }

        //初始化微信支付环境
        function fCharge() {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                fPostCharge();
            }
        }
        //提交充值数据
        function fPostCharge() {
            var vChargeVal = @Session["FormPayMon"];
            vChargeVal = parseFloat(vChargeVal);
            if (vChargeVal > 0) {
                $.ajax({
                    type: "post",
                    data: "totalfee=" + vChargeVal,
                    url: "/JsApiPay/MeterRecharge",
                    success: function (json) {
                        onBridgeReady(json);
                    },
                    error: function () {
                    }
                })
            }
            else {      
            }
        }
        //调用微信支付模块
        function onBridgeReady(json) {
            WeixinJSBridge.invoke(
           'getBrandWCPayRequest', {
               "appId": json.appId,     //公众号名称，由商户传入
               "timeStamp": json.timeStamp,         //时间戳，自1970年以来的秒数
               "nonceStr": json.nonceStr, //随机串
               "package": json.packageValue,
               "signType": "MD5",         //微信签名方式:
               "paySign": json.paySign //微信签名
           },
           function (res) {
               if (res.err_msg == "get_brand_wcpay_request:ok") {
                   //alert("支付成功,请稍后查询余额,如有疑问,请联系管理员.");
                   window.location.href='/Order/OrderList';
               }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
           }
            );
        }
    </script>
  
</html>
